from selenium.webdriver.common.by import By

from webAutomation import utils
from webAutomation.base.base_page import BasePage, BaseHandle


# 对象层
# 首页页面类
class OverviewOfBatteryPage(BasePage):
    def __init__(self):
        # 继承父类
        super().__init__()
        # 新增电池
        self.add_battery = By.XPATH, "//*[@id='app']/section/section/main/div[1]/div/div/div[1]/div[1]/button[3]/span"
        # 所属地区下拉列表
        self.area = By.XPATH, "//*[@id='app']/section/section/main/div[1]/div/div/div[1]/div[2]/div/div[2]/form/div[1]/div/div/div/input"
        # 所属地区名
        self.area_name = By.XPATH, "/html/body/div[3]/div[1]/div[1]/ul/li[3]/span"
        # 电池厂商
        self.battery_manufacturer = By.XPATH, "//*[@id='app']/section/section/main/div[1]/div/div/div[1]/div[2]/div/div[2]/form/div[2]/div/div/input"
        # 电池型号下拉列表
        self.battery_model_list = By.XPATH, "//*[@id='app']/section/section/main/div[1]/div/div/div[1]/div[2]/div/div[2]/form/div[3]/div/div/div[1]/input"
        # 电池型号
        self.battery_model = By.XPATH, "/html/body/div[4]/div[1]/div[1]/ul/li/span"
        # 电池SN码
        self.battery_sn = By.XPATH, "//*[@id='app']/section/section/main/div[1]/div/div/div[1]/div[2]/div/div[2]/form/div[4]/div/div/input"
        # 确定新增
        self.assure_add = By.XPATH, '//*[@id="app"]/section/section/main/div[1]/div/div/div[1]/div[2]/div/div[3]/div/button[2]/span'

    # 查找新增电柜按钮
    def find_add_battery(self):
        return self.find_element(self.add_battery)

    # 查找地区下拉列表
    def find_area_list(self):
        return self.find_element(self.area)

    # 查找地区名
    def find_area_name(self):
        return self.find_element(self.area_name)

    # 查找电池厂商
    def find_battery_manufacturer(self):
        return self.find_element(self.battery_manufacturer)

    # 查找电池型号下拉列表
    def find_battery_model_list(self):
        return self.find_element(self.battery_model_list)

    # 查找电池型号
    def find_battery_model(self):
        return self.find_element(self.battery_model)

    # 查找电池SN码
    def find_battery_sn(self):
        return self.find_element(self.battery_sn)

    # 确定新增电池
    def find_assure_add(self):
        return self.find_element(self.assure_add)


# 操作层
class OverviewOfBatteryHandle(BaseHandle):
    def __init__(self):
        # 实例化首页页面类
        self.overview_of_battery_page = OverviewOfBatteryPage()

    # 点击新增电池按钮
    def click_add_battery(self):
        return self.overview_of_battery_page.find_add_battery().click()

    # 点击所属地区下拉列表
    def click_area_list(self):
        return self.overview_of_battery_page.find_area_list().click()

    # 选择所属地区名
    def click_area_name(self):
        return self.overview_of_battery_page.find_area_name().click()

    # 输入电池厂商
    def input_battery_manufacturer(self, battery_manufacturer):
        return self.input_text(self.overview_of_battery_page.find_battery_manufacturer(), battery_manufacturer)

    # 点击电池型号下拉列表
    def click_battery_model_list(self):
        return self.overview_of_battery_page.find_battery_model_list().click()

    # 点击电池型号
    def click_battery_model(self):
        return self.overview_of_battery_page.find_battery_model().click()

    # 输入电池SN码
    def input_battery_sn(self, battery_sn):
        return self.input_text(self.overview_of_battery_page.find_battery_sn(), battery_sn)

    # 确定新增
    def click_assure_add(self):
        return self.overview_of_battery_page.find_assure_add().click()


# 业务层
class OverviewOfBatteryProxy:
    def __init__(self):
        self.overview_of_battery_handle = OverviewOfBatteryHandle()

    def add_battery(self, battery_manufacturer, battery_sn):
        print("---------add battery start...----------")
        # 点击新增电池按钮
        self.overview_of_battery_handle.click_add_battery()
        # 选择地区
        self.overview_of_battery_handle.click_area_list()
        # 选择地区名字
        self.overview_of_battery_handle.click_area_name()
        # 输入电池厂商
        self.overview_of_battery_handle.input_battery_manufacturer(battery_manufacturer)
        # 点击电池型号下拉列表
        self.overview_of_battery_handle.click_battery_model_list()
        # 点击电池型号
        self.overview_of_battery_handle.click_battery_model()
        # 输入电池SN码
        self.overview_of_battery_handle.input_battery_sn(battery_sn)
        # 确定新增
        self.overview_of_battery_handle.click_assure_add()
        print("----------add battery end...-----------")
